<?php

namespace App\Http\Controllers\Api;

use App\Models\Currency;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 订单管理.
 */
class OrderController extends Controller
{
    /**
     * 账单管理.
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/16
     *
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     *
     * @return void|int|string|bool|array comment
     */
    public function clearing_bank(Request $request)
    {
        $cid = $request->get('cid') ?? 2;
        //查询用户资产
        $where['cid'] = $cid;
        $top = Order::where($where)->orderBy('price', 'desc')->first(); //low
        $list = array();
        $pro = Order::where($where)->orderBy('id', 'desc')->first();

        if ($pro) {
            $list['money'] = number_format($pro->price, 2,'.','');
        } else {
            $list['money'] = 0;
        }

        $c_info = Currency::where('id', $cid)->first();

        $list['name_en'] = $c_info->name_en;
        $list['name'] = $c_info->name;
        $list['img'] = config('alioss.host').'/'.$c_info->logo;

        if (empty($top)) {
            $list['price_top'] = 0;
            $list['price_low'] = 0;
            $list['total_price'] = 0;
            $list['total_num'] = 0;
            $arr = [
                'num' => 0,
                'price' => 0,
                'type' => 3, //暂无交易
                'total_price' => 0,
                'created_at' => '-',
            ];

            return view('api/order/error', compact('list'));
        } else {
            $low = Order::where($where)->orderBy('price', 'asc')->first();

            $list['price_top'] = number_format($top->price, 2,'.','');
            $list['price_low'] = number_format($low->price, 2,'.','');
            $list['total_price'] = Order::where($where)->sum('total_price') / 2;
            $list['total_num'] = Order::where($where)->sum('num') / 2;
            $where['type'] = 1;

            $arr = Order::where($where)->orderBy('id', 'desc')->take(30)
                ->get(['cid', 'num', 'price', 'type', 'total_price', 'created_at']);

            foreach ($arr as $k => $v) {
                $list['data'][$k]['price'] = number_format($v->price, 2,'.','');
                $list['data'][$k]['type'] = $v->type;
                $list['data'][$k]['created_at'] = $v->created_at;
                if ($c_info['is_standard'] == 2) {
                    $list['data'][$k]['total_price'] = number_format($v->total_price, 2);
                } else {
                    $list['data'][$k]['total_price'] = number_format($v->total_price, 6);
                }

                if ($v->currency->name_en == 'OBA') {
                    $list['data'][$k]['num'] = number_format($v->num, 0,'.','');
                } else {
                    $list['data'][$k]['num'] = number_format($v->num, 4,'.','');
                }
            }
        }

        return view('api/order/clearing_bank', compact('list'));
    }

    /**
     * 订单列表
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/29
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function order_index(Request $request)
    {
        $uid = $request->get('uid');
        $type = $request->get('type');
        $page = $request->get('page');
        if (!empty($page)) {
            $page = $page - 1;
        } else {
            $page = 0;
        }
        if (empty($uid) || empty($type))
            return respF(1003);
        $where['uid'] = $uid;
        switch ($type) {
            case 1:
                //end
                $start_time = date('Y-m-d', time());
                $end_time = date('Y-m-d', time() + 86400);
                break;
            case 2:
                $e_time = $request->get('end_time');
                $start_time = $request->get('start_time') ?? '2000-0-0';
                $end_time = $e_time ? date('Y-m-d',strtotime($e_time)+ 86400) :date('Y-m-d', time() + 84000);
                break;
            default:
                return respF(1003);
        }
        $arr = Order::where($where)->whereBetween('created_at', [$start_time, $end_time])->orderBy('created_at', 'desc')->select(['id', 'price', 'type', 'total_price', 'cid', 'real_price', 'num', 'price', 'created_at'])->paginate(10);
        if (count($arr) < 1)
            return respF(1009);
        $arrs = collect($arr->items());
        foreach ($arrs as $k => $v) {
            $currency = Currency::where('id', $v['cid'])->first();
            $unit = $currency->unit;
            $arrs[$k]['name_en'] = $currency->name_en;
            $arrs[$k]['created_at'] = strtotime($v['created_at']);
            $arrs[$k]['total_price'] = number_format($v['total_price'],2,'.','');
            $arrs[$k]['num'] = number_format($v['num'],$unit,'.','');
            $arrs[$k]['price'] = number_format($v['price'],2,'.','');
            $arrs[$k]['real_price'] = number_format($v['real_price'],2,'.','');
        }
        $list['status'] = 1;
        $list['msg'] = '查询成功';
        $list['data'] = $arrs;
        return response()->json($list);
    }
}
